<?php
declare (strict_types = 1);
namespace app\admin\model;

use app\admin\model\Common;
use app\admin\model\AuthRule;

class AuthGroup extends Common
{
	
	/**
	 * 修改保存数据
	 */
	public function savedata($data){
		if(!empty($data)){
			if(!empty($data)){
				$data['rules'] = implode(',',$data['rules']);
			}
			if(!empty($data['id'])){
				$id = $this->update($data, ['id' => $data['id']]);
				if(!empty($id)){
					return [
						'status'=>1,
						'notice'=>'操作成功！',
					];
				} else {
					return [
						'status'=>2,
						'notice'=>'操作失败！',
					];
				}
			} else{
				$id = $this->save($data);
				if(!empty($id)){
					return [
						'status'=>1,
						'notice'=>'操作成功！',
					];
				} else {
					return [
						'status'=>2,
						'notice'=>'操作失败！',
					];
				}
			}
		} else {
			return [
				'status'=>2,
				'notice'=>'数据不能为空！',
			];
		}
	}
	
	/**
	 * 获取器
	 */
	public function getStatusAttr($value){
		//return $this->fields['status']['options'][$value];
		$config = config('tables.auth_group.fields.status.options');
		return $config[$value];
	}

	/**
	 * 获取规则
	 */
	public function getRulesAttr($value){
		$rulesarr = explode(",",$value);
		$lists = AuthRule::where('id','in',$rulesarr)->column("title","id");
		return $lists;
	}
	
	/**
	 * 获取规则
	 */
	public function getRulessAttr($value,$data)
	{
		$rulesarr = explode(",",$data['rules']);
		$lists = AuthRule::where('id','in',$rulesarr)->column("id");
		return $lists;
	}
}
